package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArtilceIsDownListener {

    @Autowired
    private ApArticleConfigMapper configMapper;

    @KafkaListener(topics = "wmNews_down_up")
    public void onMessage(String message) {
        if (StringUtils.isNotBlank(message)) {
            //把字符串转换成map对象
            Map map = JSON.parseObject(message, Map.class);
            Integer articleId = (Integer) map.get("articleId");
            Short enable = (Short) map.get("enable");
            //查询文章配置表
            ApArticleConfig apArticleConfig = configMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId,
                    articleId));
            //修改状态
            if (enable == 1) {
                apArticleConfig.setIsDown(false);
            } else {
                apArticleConfig.setIsDown(true);
            }
            configMapper.updateById(apArticleConfig);
            log.info("article端文章配置修改，articleId={}", map.get("articleId"));
        }
    }
}